import Cell from '../components/expand'
import FormatterComponent from '@/components/FormatterView'

export default {
  components: {
    Cell,
    FormatterComponent
  },
  props: {
    // 表格配置
    gridConfig: {
      type: Array,
      default() {
        return []
      }
    },
    // 表格死数据
    showGridData: {
      type: Array,
      default() {
        return []
      }
    },
    // 判断是否是异步数据
    listLoading: {
      type: Boolean,
      default: false
    },
    // 判断是否是异步数据
    lazy: {
      type: Boolean,
      default: false
    },
    //  是否隐藏表格操作
    hideEditArea: {
      type: Boolean,
      default: false
    },
    // 是否多选表格
    multiple: {
      type: Boolean,
      default: false
    },
    // 表格按钮配置
    gridHandles: {
      type: Array,
      default() {
        return []
      }
    },
    size: {
      type: String,
      default: 'small'
    },
    //  表格编辑区域宽度
    gridEditWidth: {
      type: Number,
      default: 0
    },
    treeProps: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      checkAll: false,
      isIndeterminate: false
    }
  },
  computed: {
    gridColumns() {
      return this.gridConfig.filter(item => {
        return !item.hidden
      })
    },
    visibleHandle() {
      return !this.hideEditArea && this.gridHandles.length > 0
    },
    validateHandles() {
      return row => {
        return this.gridHandles.filter(btn => {
          if (btn.filter) {
            if (typeof btn.filter === 'function') {
              return btn.filter(btn, row) || false
            } else {
              return row[btn.filter] || false
            }
          } else {
            return true
          }
        })
      }
    }
  },
  methods: {
    // 处理相应父组件的事件方法
    handleEmit(emitName, row) {
      this.$emit('action', emitName, row)
    },
    // 处理相应父组件的事件方法
    onCellClick($event) {
      const { name, type, row } = $event
      if (type === 'handles') {
        this.handleEmit(name, row)
      } else {
        this.handleEmit(name, row)
      }
    },
    handleSelectionChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.showGridData.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.showGridData.length
      this.$emit('select', value)
    },
    toggleAllMultipleSelection() {
      if (this.$refs.gridTable) {
        this.$refs.gridTable.toggleAllSelection()
      }
    }
  }
}
